<template>
  <div class="login-container">
    <div class="login-banner">
      <div class="banner-content">
        <h1 class="banner-title">生产执行管理系统</h1>
        <p class="banner-subtitle">高效、智能、可靠的生产管理平台</p>
        <div class="banner-features">
          <div class="feature-item">
            <i class="el-icon-data-line feature-icon"></i>
            <span>实时数据监控</span>
          </div>
          <div class="feature-item">
            <i class="el-icon-setting feature-icon"></i>
            <span>智能生产调度</span>
          </div>
          <div class="feature-item">
            <i class="el-icon-document-checked feature-icon"></i>
            <span>质量追溯管理</span>
          </div>
        </div>
      </div>
    </div>
    
    <div class="login-form-wrapper">
      <div class="login-form-container">
        <div class="form-header">
          <h2>欢迎登录</h2>
          <p>请输入您的账号和密码</p>
        </div>
        
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input 
              v-model="loginForm.username" 
              type="text" 
              auto-complete="off" 
              placeholder="请输入账号"
              prefix-icon="el-icon-user"
              size="medium">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input 
              v-model="loginForm.password" 
              type="password" 
              auto-complete="off" 
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
              size="medium"
              @keyup.enter.native="handleLogin">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="code" v-if="captchaOnOff">
            <div class="captcha-container">
              <el-input 
                v-model="loginForm.code" 
                auto-complete="off" 
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
                size="medium"
                @keyup.enter.native="handleLogin">
              </el-input>
              <div class="captcha-image">
                <img :src="codeUrl" @click="getCode" class="login-code-img" alt="验证码" />
              </div>
            </div>
          </el-form-item>
          
          <el-form-item>
            <el-checkbox v-model="loginForm.rememberMe" size="medium">记住密码</el-checkbox>
            <a href="javascript:;" class="forgot-password">忘记密码？</a>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              :loading="loading" 
              size="medium" 
              type="primary" 
              class="login-button"
              @click.native.prevent="handleLogin">
              {{ loading ? '登录中...' : '登录' }}
            </el-button>
          </el-form-item>
          
          <el-form-item v-if="register" class="register-link">
            <span>还没有账号？</span>
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </el-form-item>
        </el-form>
      </div>
      
      <div class="login-footer">
        <span>Copyright &copy; 2024 MES All Rights Reserved.</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    toIPC() {
      window.open("https://beian.miit.gov.cn/", "_blank");
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => { });
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login-container {
  display: flex;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-banner {
  flex: 1;
  background: linear-gradient(120deg, #2d6bb0, #1a3f7c);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  
  &::before {
    content: "";
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, rgba(255,255,255,0) 70%);
    transform: rotate(30deg);
  }
  
  .banner-content {
    text-align: center;
    z-index: 1;
    max-width: 600px;
    padding: 20px;
  }
  
  .banner-title {
    font-size: 36px;
    font-weight: 600;
    margin-bottom: 20px;
    letter-spacing: 2px;
  }
  
  .banner-subtitle {
    font-size: 18px;
    margin-bottom: 40px;
    opacity: 0.9;
  }
  
  .banner-features {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: 30px;
  }
  
  .feature-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    
    .feature-icon {
      font-size: 28px;
      margin-bottom: 10px;
    }
  }
}

.login-form-wrapper {
  width: 450px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: white;
  position: relative;
  box-shadow: -2px 0 10px rgba(0,0,0,0.1);
}

.login-form-container {
  width: 320px;
  padding: 40px 20px;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
  
  h2 {
    font-size: 24px;
    color: #333;
    margin-bottom: 10px;
  }
  
  p {
    color: #999;
    font-size: 14px;
  }
}

.login-form {
  .el-form-item {
    margin-bottom: 22px;
  }
  
  .el-input {
    height: 42px;
    
    ::v-deep .el-input__inner {
      height: 42px;
      line-height: 42px;
      border-radius: 4px;
    }
  }
  
  .captcha-container {
    display: flex;
    gap: 10px;
    
    .el-input {
      flex: 1;
    }
    
    .captcha-image {
      width: 120px;
      
      .login-code-img {
        width: 100%;
        height: 42px;
        border: 1px solid #dcdfe6;
        border-radius: 4px;
        cursor: pointer;
        object-fit: cover;
      }
    }
  }
  
  .forgot-password {
    float: right;
    font-size: 13px;
    color: #409EFF;
    text-decoration: none;
    
    &:hover {
      color: #66b1ff;
    }
  }
  
  .login-button {
    width: 100%;
    height: 42px;
    margin-top: 10px;
    background: linear-gradient(120deg, #2d6bb0, #1a3f7c);
    border: none;
    font-size: 16px;
    letter-spacing: 2px;
  }
  
  .register-link {
    text-align: center;
    margin-top: 20px;
    margin-bottom: 0;
    font-size: 14px;
    
    .link-type {
      color: #409EFF;
      text-decoration: none;
      margin-left: 5px;
      
      &:hover {
        color: #66b1ff;
        text-decoration: underline;
      }
    }
  }
}

.login-footer {
  position: absolute;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: #999;
  font-size: 12px;
}

@media (max-width: 992px) {
  .login-banner {
    display: none;
  }
  
  .login-form-wrapper {
    width: 100%;
    justify-content: center;
  }
}
</style>